/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ipartek.formacion.gestformwww.controlador;

import com.ipartek.formacion.gestformwww.modelo.ModeloEntrega;
import com.ipartek.formacion.gestformwww.modelo.beans.Entrega;
import com.ipartek.formacion.gestformwww.modelo.beans.excepciones.GestorFormacionExcepcion;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Santi
 */
public class ModificarEntregaAccion implements Accion {

    private final String modificarEntregaJSP = "modificar-entrega";

    public AccionRouter ejecuta(HttpServlet servlet, HttpServletRequest request, HttpServletResponse response) {

        AccionRouter router = null;
        ModeloEntrega modeloEntrega = null;
        int entId = 0;
        //int idCurso = 0;
        boolean correcto = false;
        Entrega entrega = null;

        if (request.getSession().getAttribute("usuario") != null) {

            if (request.getParameter("entId") != null) {
                entId = Integer.parseInt(request.getParameter("entId"));
            } else if (request.getAttribute("entId") != null) {
                entId = Integer.parseInt(request.getAttribute("entId").toString());
            }
            //esperando saber si util:
//            if (request.getParameter("idCurso") != null) {
//                idCurso = Integer.parseInt(request.getParameter("idCurso"));
//            } else if (request.getAttribute("idCurso") != null) {
//                idCurso = Integer.parseInt(request.getAttribute("idCurso").toString());
//            }

            modeloEntrega = (ModeloEntrega) servlet.getServletContext().getAttribute("ModeloEntrega");

            try {
                entrega = modeloEntrega.getEntrega(entId);
                correcto = true;
            } catch (GestorFormacionExcepcion ex) {
                Logger.getLogger(ModificarEntregaAccion.class.getName()).log(Level.SEVERE, null, ex);
            }

            if (correcto) {
                request.setAttribute("entrega", entrega);
                request.setAttribute("contenido", modificarEntregaJSP);
                
                router = new AccionRouter("principal");
            } else {
                router = new AccionRouter("inicio");
            }



        } else {
            router = new AccionRouter("inicio");
        }


        return router;
    }
}
